module.exports = (mod)->
    do ->
        if not window.TencentCaptcha
            $.getScript("https://ssl.captcha.qq.com/TCaptcha.js")
    (form, url, data, success, error)->
        appid = await mod_appid(mod)
        form = $ form
        tencent_captcha = ->
            (new TencentCaptcha(
                appid
                (res)=>
                    if not res.ret
                        {ticket,appid,randstr} = res
                        form.save(
                            url
                            {
                                ticket,appid,randstr
                                ... data
                            }
                            success
                            (err)->
                                if err.captcha
                                    tencent_captcha()
                                    return {}
                                if error
                                    return error.apply @,arguments
                                return err

                        )
            )).show()

        tencent_captcha()

_APPID = {}

mod_appid = (mod)->
    if not window.TencentCaptcha
        await $.getScript("https://ssl.captcha.qq.com/TCaptcha.js")

    appid = _APPID[mod]
    if not appid
        _APPID[mod] = appid = (await $.get("*yun/captcha/"+mod))+""
    return appid



